﻿using System;
using System.Threading;

namespace Numble
{
    class StartScreen
    {
        public static int time = 1000;
        public static Random next = new Random();

        public static int ChooseLevel(int size)
        {
            int startN = size;
            int level = 0;

            try
            {
                size *= 2;
                Console.SetCursorPosition(0, --size);
                Console.WriteLine(" PLEASE CHOOSE LEVEL FROM 1 TO 10");
                Thread.Sleep(time);
                size++;
                Console.SetCursorPosition(0, ++size);
                Console.Write("      PLEASE ENTER LEVEL: ");
                level = int.Parse(Console.ReadLine());
                if (level < 1 || level > 10)
                {
                    Console.SetCursorPosition(0, size);

                    throw new ArgumentException();
                }
            }
            catch (ArgumentException)
            {
                size++;
                Console.SetCursorPosition(0, ++size);
                size++;
                //Thread.Sleep(time);
                Console.Write("  PLEASE CHOOSE CORRECT LEVEL!");
                Thread.Sleep(time);
                Console.Clear();
                return ChooseLevel(startN);
                //Thread.Sleep(time);
            }
            catch (FormatException)
            {
                size++;
                Console.SetCursorPosition(0, ++size);
                size++;
                //Thread.Sleep(time);

                Console.Write("     A NUMBER IS EXPECTED!");
                Thread.Sleep(time);
                Console.Clear();
                return ChooseLevel(startN);
            }
            Console.Clear();

            CountDown(startN);
            return level;
        }

        private static void CountDown(int n)
        {

            int heigth = n;
            int column = n + 3;
            int time = 1000;

            heigth += n;
            column += 0;
            Print3(n, heigth, column);
            Thread.Sleep(time);
            Console.Clear();

            Print2(n, heigth, column);
            Thread.Sleep(time);
            Console.Clear();

            Print1(n, heigth, column);
            Thread.Sleep(time);
            Console.Clear();


            // printing S
            heigth = 0;
            column = n + 3;
            PrintS(n, heigth, column);

            ////print T
            heigth += n;
            column += 0;
            Console.ForegroundColor = ConsoleColor.Cyan;

            PrintT(n, heigth, column);

            //// printing a
            heigth += n;
            column += 0;
            PrintA(n, heigth, column);
            // printing r
            heigth += n;
            column += 0;
            PrintR(n, heigth, column);
            ////print T
            heigth += n;
            column += 0;
            Console.ForegroundColor = ConsoleColor.Red;

            PrintT(n, heigth, column);
            Thread.Sleep(time);
            Console.Clear();
        }

        private static void Print3(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Green;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (((row == 0 || row == n / 2 || row == matrix.GetLength(0) - 1) && (col >= 0 && col < matrix.GetLength(1) - 1)) ||
                        (col == matrix.GetLength(1) - 1 && ((row > 0 && row < n / 2) || (row > n / 2 && row < matrix.GetLength(0) - 1))))
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine();
        }

        private static void Print2(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Yellow;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (((row == 0 || row == n / 2 ||
                        row == matrix.GetLength(0) - 1) && (col > 0 && col < matrix.GetLength(1) - 1)) ||
                        (col == matrix.GetLength(1) - 1 && row > 0 && row < n / 2) ||
                        (col == 0 && row < matrix.GetLength(0) - 1 && row > n / 2))
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }
            Console.ForegroundColor = ConsoleColor.Black;

            Console.WriteLine();

        }

        private static void Print1(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Red;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if ((row >= 0 && row < matrix.GetLength(0)) && col == matrix.GetLength(1) / 2)
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }
                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine();
        }

        static void PrintS(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Blue;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (((row == 0 || row == n / 2 || row == matrix.GetLength(0) - 1) && (col > 0 && col < matrix.GetLength(1) - 1)) || (col == 0 && row > 0 && row < n / 2) || (col == matrix.GetLength(1) - 1 && row < matrix.GetLength(0) - 1 && row > n / 2))
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }

                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine();
        }

        static void PrintT(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (row == 0 || col == (n - 2) / 2)
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }
            Console.WriteLine();
        }

        static void PrintA(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Green;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (row + col == (n - 2) / 2 || col - row == (n - 2) / 2 || (col == 0 && row >= n / 2) || (col == matrix.GetLength(1) - 1 && row >= n / 2) || row == n / 2)
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine();
        }

        static void PrintR(int n, int heigth, int column)
        {
            string[,] matrix = new string[n, (n - 2)];
            Console.ForegroundColor = ConsoleColor.Yellow;

            for (int row = 0; row < matrix.GetLength(0); row++)
            {
                Console.SetCursorPosition(heigth, column++);

                for (int col = 0; col < matrix.GetLength(1); col++)
                {
                    if (col == 0 || ((row == 0 || row == n / 2) && col < matrix.GetLength(1) - 1) || (col == matrix.GetLength(1) - 1 && row >= 1 && row < n / 2) || (row > n / 2 && col >= (n - 2) / 2 && row - col == 2))
                    {
                        int num = next.Next(0, 10);
                        Console.Write(num);
                    }
                    else
                    {
                        Console.Write(" ");
                    }

                }
                Console.WriteLine();
            }

            Console.ForegroundColor = ConsoleColor.Black;
            Console.WriteLine();
        }
    }
}
